﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
//
using PerceptiveMCAPI;
//

namespace PerceptiveMCAPI_Test
{
   public partial class get_generateText : System.Web.UI.UserControl
   {
      public ApiMethod method { get; set; }

      public object content
      {
         get { return contentFormat(); }
      }
      public EnumValues.generateText_type generateTextType
      {
         get { return ApiHelper.GetEnumFromString<EnumValues.generateText_type>( ddlGenTextType.SelectedItem.Text ); }
         private set { ddlGenTextType.SelectedValue = Convert.ToInt32( value ).ToString(); }
      }
      public void Setup()
      {
         this.Visible = true;

         lblMethod.Text = method.name;
         lblSection.Text = method.section;

         ddlGenTextType.DataSource = EnumValues.generateTextTypeValues();
         ddlGenTextType.DataBind();
         generateTextType = MCAPISettings.default_generateText_type;
         // set 1st time based on default
         ddlGenTextType_SelectedIndexChanged( new object { }, EventArgs.Empty );
         // clear template on setup, but leave alone if change between content types
         sub_TemplateSections1.TemplateSections = new Dictionary<string, string>();
      }

      protected void ddlGenTextType_SelectedIndexChanged( object sender, EventArgs e )
      {
         setDefaults();
         //
         if ( ddlGenTextType.SelectedItem.Text == "html" )
         {
            tbContent.TextMode = TextBoxMode.MultiLine;
         }
         else if ( ddlGenTextType.SelectedItem.Text == "template" )
         {
            divContentTemplate.Visible = true;
            tbContent.Visible = false;
         }
      }
      // -----------------------------------------
      private void setDefaults()
      {
         tbContent.Text = "";
         tbContent.Visible = true;
         tbContent.TextMode = TextBoxMode.SingleLine;
         tbContent.Focus();
         //
         divContentTemplate.Visible = false;
      }
      // -----------------------------------------
      private object contentFormat()
      {
         object content = null;
         //
         if ( ddlGenTextType.SelectedItem.Text == "template" )
         {
            Dictionary<string, string> template = sub_TemplateSections1.TemplateSections;

            content = template;
         }
         else
         {
            content = tbContent.Text.Trim();
         }
         //
         return content;
      }

   }
}